import solve from "../../utils/solve";
import DifficultyEnum from "../../utils/DifficultyEnum";
import timerUtil from "../../utils/timerUtil";

// pages/hello/first.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        sudokuSelectIndex: {
            x: -1,
            y: -1,
        },
        currBlock: -1,
        originalData: [],
        sudokuBeforeDataCopy: [],
        sudokuDataCopy: [],
        solvedDate: [],
        difficulty: DifficultyEnum.EASY,
        seconds: 0,
        timerInterval: null,
        isPause: false,
    },

    /**
     * 补全数独
     */
    solveSudoku: function () {

        this.setData({
            sudokuBeforeDataCopy: solve.tdDeepCopy(this.data.sudokuDataCopy),
            sudokuDataCopy: solve.tdDeepCopy(this.data.solvedDate),
        })
        // console.log('sudokuData', this.data.originalData)
        // console.log('sudokuDataCopy', this.data.sudokuDataCopy)
    },
    /**
     * 恢复之前的数组
     */
    recoverBefore: function () {
        this.setData({
            sudokuDataCopy: solve.tdDeepCopy(this.data.sudokuBeforeDataCopy)
        })
    },

    /**
     * 获取数独选中的下标
     */
    sudokuIndex: function (options) {
        // console.log(options.target.dataset)
        const indexObj = {...options.target.dataset}
        this.setData({
            sudokuSelectIndex: {
                ...indexObj
            }
        })
        // console.log('selectIndex', this.data.sudokuSelectIndex)
    },

    /**
     * 获得选中的
     */
    selectBtn: function (itemEvent) {
        // console.log(itemEvent.target.dataset)
        const selected = itemEvent.target.dataset.selectitm
        //console.log('select value=>0',this.data.originalData[this.data.sudokuSelectIndex.x][this.data.sudokuSelectIndex.y] !== 0)
        if (
            this.data.originalData[this.data.sudokuSelectIndex.x][this.data.sudokuSelectIndex.y] === 0
        ) {
            const modifyObj = `sudokuDataCopy[${this.data.sudokuSelectIndex.x}][${this.data.sudokuSelectIndex.y}]`
            this.setData({
                [modifyObj]: selected
            })
        }
    },

    /**
     * 换一组数独
     */
    changSudoku: function () {
        let original = solve.initSudokuArr(9, this.data.difficulty)
        this.setData({
            originalData: solve.tdDeepCopy(original)
        })

        this.setData({
            sudokuDataCopy: solve.tdDeepCopy(original)
        })

        let tempSudoKuArr = solve.tdDeepCopy(this.data.originalData)
        solve.solveSudoku(tempSudoKuArr)
        this.setData({
            solvedDate: solve.tdDeepCopy(tempSudoKuArr)
        })

        timerUtil.clearTimer(this.data.timerInterval)
        this.setData({
            seconds: 0
        })
        //开始计时
        const interval = timerUtil.getTimer(() => {
            this.setData({
                seconds: this.data.seconds + 1
            })
        }, 1000)
        //设置计时器 为了之后取消计时器
        this.setData({
            timerInterval: interval
        })
    },

    /**
     * 清楚当前选中项
     */
    clearOneItem: function () {
        const to = {...this.data.sudokuSelectIndex}
        const modifyObj = `sudokuDataCopy[${to.x}][${to.y}]`

        if (to.x !== -1 && to.y !== -1 && this.data.originalData[to.x][to.y] === 0) {
            this.setData({
                [modifyObj]: 0
            })
        }
    },

    /**
     * 清空数独
     */
    clearSudoku: function () {
        this.setData({
            sudokuDataCopy: solve.tdDeepCopy(this.data.originalData)
        })
    },
    /**
     * 暂停数独
     */
    sudokuPause: function () {
        const pause = this.data.isPause
        if (!pause) {
            timerUtil.clearTimer(this.data.timerInterval)
            this.setData({
                isPause: true
            })
            wx.showModal({
                title: '暂停游戏',
                content: '暂停游戏',
                showCancel: false,
                confirmText: '继续游戏',
                success: (res) => {
                    if (!res.cancel) {
                        //开始计时
                        const interval = timerUtil.getTimer(() => {
                            this.setData({
                                seconds: this.data.seconds + 1
                            })
                        }, 1000)
                        //设置计时器 为了之后取消计时器
                        this.setData({
                            timerInterval: interval
                        })
                        this.setData({
                            isPause: false
                        })
                    }

                }
            })
        }
    },
    /**
     * 判断数独完成
     */
    confirmSudoku: function () {
        //先判断数独是否填满
        const isComplete = solve.isCompleteSudoku(this.data.sudokuDataCopy)
        if (!isComplete) {
            wx.showToast({
                title: '数独尚未完成',
                duration: 2000,
                mask: true,
                icon: 'loading',
            })
        } else {
            const isValid = solve.isValidSudoku(this.data.sudokuDataCopy)
            if (isValid) {
                timerUtil.clearTimer(this.data.timerInterval)
                wx.showModal({
                    title: '完成游戏',
                    content: `恭喜你完成游戏! 用时${solve.dataFormat(this.data.seconds)}`,
                    showCancel: true,
                    confirmText: '新游戏',
                    cancelText: '结束游戏',
                    success: (res) => {
                        if (res.cancel) {
                            // console.log('点击立取消')
                            // 返回难度选择界面
                            wx.redirectTo(
                                {
                                    url: "../index2/index2",
                                }
                            )
                        } else if (res.confirm) {
                            // console.log('点击了确定')
                            this.changSudoku()
                        }
                    }
                })
            } else {
                wx.showToast({
                    title: '数独有错误',
                    duration: 2000,
                    mask: true,
                    icon: 'loading',
                })
            }

        }
    },

    /**
     *模拟完成整个数独 这个函数到时删除
     */
    completeSudoku: function () {
        this.setData({
            sudokuDataCopy: solve.tdDeepCopy(this.data.solvedDate)
        })
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        const d = options.difficulty
        // console.log('难度=>', d)

        this.setData({
            difficulty: d
        })

        let ta = solve.initSudokuArr(9, d)
        this.setData({
            originalData: solve.tdDeepCopy(ta)
        })

        this.setData({
            sudokuDataCopy: solve.tdDeepCopy(ta)
        })
        let tempSudoKuArr = solve.tdDeepCopy(this.data.originalData)
        solve.solveSudoku(tempSudoKuArr)
        this.setData({
            solvedDate: solve.tdDeepCopy(tempSudoKuArr)
        })
        //开始计时
        const interval = timerUtil.getTimer(() => {
            this.setData({
                seconds: this.data.seconds + 1
            })
        }, 1000)
        //设置计时器 为了之后取消计时器
        this.setData({
            timerInterval: interval
        })
    },


})